/**
 * 播放音频
 */
let IA = null; // uni.createInnerAudioContext()

export default class InnerAudio {
	constructor() {}

	static stop() {
		if (IA) {
			IA.stop();
		}
	}

	static onTimeUpdates() {
		return IA;
	}
	
	static canPlay(src) {
		return new Promise((resolve, reject) => {
			if (!src) {
				reject('200');
				return;
			}
	
			if (IA) {
				IA.destroy();
			}
	
			IA = uni.createInnerAudioContext();
			const srcs = src.split(',');
			let len = 0;
	
			IA.src = srcs[len];
	
			IA.onCanplay(res => {
				resolve(IA)
			})
	
			IA.onError((err) => {
				reject('err');
			});
	
		});
	}

	static play(src) {
		return new Promise((resolve, reject) => {
			
			IA.play();
			
			IA.onEnded(() => {
				resolve('end');
			});

			IA.onError((err) => {
				console.log(err, '播放异常');
				reject('err');
			});

		});
	}
}